package com.litchi.activity;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.GetListener;

import com.litchi.R;
import com.lib.uil.ScreenUtils;
import com.lib.uil.ToastUtils;
import com.lib.uil.UILUtils;
import com.litchi.app.MyApplication;
import com.litchi.bean.Goods;
import com.litchi.bean.GoodsHistory;
import com.litchi.utils.Constants;
import com.litchi.utils.DBUtils;
import com.litchi.utils.NumberUtils;
import com.litchi.utils.SwipeMenuListView.SwipeMenu;
import com.litchi.utils.SwipeMenuListView.SwipeMenuCreator;
import com.litchi.utils.SwipeMenuListView.SwipeMenuItem;
import com.litchi.utils.SwipeMenuListView.SwipeMenuListView;
import com.litchi.utils.SwipeMenuListView.SwipeMenuListView.OnMenuItemClickListener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class FavorActivity extends Activity implements OnClickListener {

	private final String strGoods = "您还没有关注过任何商品！何不去逛逛~";
	private final String strStore = "您还没有关注过任何店铺！何不去逛逛~";
	private ArrayList<GoodsHistory> mData = new ArrayList<GoodsHistory>();
	private FavorAdapter mAdapter;
	private SwipeMenuListView mListView;
	private ProgressBar mBar;
	private View layoutNull;
	private TextView mTvGoods;
	private TextView mTvStore;
	 
	private TextView mBtnMore;
	private TextView mTvDisc;
	private MyApplication mApplication;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_favor);
		mApplication = (MyApplication) getApplicationContext();
		mContext = this;
		initData();
		initListView();
		mBar = (ProgressBar) findViewById(R.id.progressBar1);
		layoutNull = findViewById(R.id.layout_null);
		mTvDisc = (TextView) findViewById(R.id.tv_disc);
		 
		findViewById(R.id.img_back).setOnClickListener(this);
		mBtnMore = (TextView) findViewById(R.id.btn_more);
		mBtnMore.setOnClickListener(this);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		initData();
	}

	private void initListView() {
		mListView = (SwipeMenuListView) findViewById(R.id.favor_listView);
		mAdapter = new FavorAdapter();
		mListView.setAdapter(mAdapter);
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				SwipeMenuItem deleteItem = new SwipeMenuItem(FavorActivity.this);
				deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
						0x3F, 0x25)));
				deleteItem.setWidth(ScreenUtils
						.getScreenWidth(FavorActivity.this) / 3);
				deleteItem.setTitle("删除");
				deleteItem.setTitleSize(18);
				deleteItem.setTitleColor(Color.WHITE);
				menu.addMenuItem(deleteItem);

			}
		};
		mListView.setMenuCreator(creator);
		mListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {
				deleteItem(position);
			}
		});
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				String goodsId = mData.get(position).getGoodsId();
				BmobQuery<Goods> query = new BmobQuery<Goods>();
				query.getObject(mContext, goodsId, new GetListener<Goods>() {

					@Override
					public void onSuccess(Goods arg0) {
						// TODO Auto-generated method stub

						Intent intent = new Intent(FavorActivity.this,
								DetailActivity.class);
						intent.putExtra(Constants.GOODS_KEY, arg0);
						startActivityForResult(intent,
								Constants.INTENT_KEY.REQUEST_CART_TO_DETAIL);

					}

					@Override
					public void onFailure(int code, String arg0) {
						// TODO Auto-generated method stub

						ToastUtils.showToast(mContext, "请检查网络连接" + arg0);
					}

				});

			}
		});
	}

	private void initData() {
		new FavorTask().execute();
	}

	/**
	 * 删除行
	 */
	private void deleteItem(int position) {
		DBUtils.delete(mData.get(position));
		mData.remove(position);
		if (mData.size() == 0) {
			layoutNull.setVisibility(View.VISIBLE);
		}
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_back:
			finish();
			break;
		case R.id.btn_more:
			gotoHomePage();
			break;
		default:
			break;
		}
	}

	private void goodsClick() {
		if (mData.size() > 0) {
			mListView.setVisibility(View.VISIBLE);
			layoutNull.setVisibility(View.GONE);
		} else {
			layoutNull.setVisibility(View.VISIBLE);
		}
		mBtnMore.setText("去首页逛逛");
		mTvDisc.setText(strGoods);
		mTvGoods.setTextColor(Color.RED);
		mTvStore.setTextColor(getResources().getColor(R.color.dark));
		 
	}

	private void stroeClick() {
		layoutNull.setVisibility(View.VISIBLE);
		mListView.setVisibility(View.GONE);
		mBtnMore.setText("去好店铺逛逛");
		mTvDisc.setText(strStore);
		mTvStore.setTextColor(Color.RED);
		mTvGoods.setTextColor(getResources().getColor(R.color.dark));
		 
	}

	/**
	 * 商品列表
	 */
	private void gotoHomePage() {
		startActivity(new Intent(this, MainActivity.class));
		Intent intent = new Intent();
		intent.setAction(Constants.BROADCAST_FILTER.FILTER_CODE);
		intent.putExtra(Constants.BROADCAST_FILTER.EXTRA_CODE,
				Constants.INTENT_KEY.FROM_FAVOR);
		sendBroadcast(intent);
		finish();
		overridePendingTransition(0, 0);
	}

	class FavorAdapter extends BaseAdapter {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View inflate = null;
			ViewHolder holder = null;
			if (convertView == null) {
				inflate = getLayoutInflater().inflate(R.layout.item_favor_list,
						null);
				holder = new ViewHolder();
				holder.imgGoods = (ImageView) inflate
						.findViewById(R.id.img_icon);
				holder.tvGoodsName = (TextView) inflate
						.findViewById(R.id.tv_title);
				holder.tvGoodsPrice = (TextView) inflate
						.findViewById(R.id.tv_price);
				inflate.setTag(holder);
			} else {
				inflate = convertView;
				holder = (ViewHolder) inflate.getTag();
			}
			GoodsHistory info = mData.get(position);
			holder.tvGoodsName.setText(info.getGoodsName());
			holder.tvGoodsPrice.setText(NumberUtils.formatPrice(info
					.getGoodsPrice()));

			mApplication.imgLoader.displayImage(
					Constants.HTTP_FILE + info.getGoodsIcon(), holder.imgGoods);

			return inflate;
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

	}

	class ViewHolder {
		ImageView imgGoods;
		TextView tvGoodsName;
		TextView tvGoodsPrice;
	}

	class FavorTask extends AsyncTask<Void, Void, List<GoodsHistory>> {

		@Override
		protected List<GoodsHistory> doInBackground(Void... params) {
			return DBUtils.getFavor();
		}

		@Override
		protected void onPostExecute(List<GoodsHistory> result) {
			super.onPostExecute(result);
			mData.clear();
			mData.addAll(result);
			mAdapter.notifyDataSetChanged();
			if (mData.size() == 0) {
				layoutNull.setVisibility(View.VISIBLE);
				mListView.setVisibility(View.GONE);
			} else {
				layoutNull.setVisibility(View.GONE);
				mListView.setVisibility(View.VISIBLE);
			}
			mBar.setVisibility(View.GONE);
		}

	}

}
